public final class EntityDefinition {
	private static int anInt56;
	private static Packet buffer;
	private static EntityDefinition[] cache;
	public static Client clientInstance;
	public static Cache npcCache = new Cache(30);
	private static int[] streamIndices;
	public boolean aBoolean84;
	public boolean aBoolean87;
	public boolean aBoolean93;
	public byte aByte68;
	public String actions[];
	public int anInt55;
	private int anInt57;
	public int anInt58;
	private int anInt59;
	public int anInt75;
	public int anInt79;
	public int anInt83;
	private int anInt85;
	private int anInt86;
	private int anInt91;
	private int anInt92;
	private int[] dialogueModels;// anIntArray73
	public int childrenIDs[];
	public int combatLevel;
	public byte description[];
	private int[] models;// anIntArray94
	private int[] modifiedModelColors;// anIntArray70
	public String name;
	private int[] originalModelColors;// anIntArray76
	public int standAnim;// anInt177
	public long type;
	public int walkAnim;// anInt67

	public static EntityDefinition forID(int i) {
		for (int j = 0; j < 20; j++) {
			if (EntityDefinition.cache[j].type == i) {
				return EntityDefinition.cache[j];
			}
		}
		EntityDefinition.anInt56 = (EntityDefinition.anInt56 + 1) % 20;
		EntityDefinition entityDefinition = EntityDefinition.cache[EntityDefinition.anInt56] = new EntityDefinition();
		EntityDefinition.buffer.offset = EntityDefinition.streamIndices[i];
		entityDefinition.type = i;
		entityDefinition.readValues(EntityDefinition.buffer);
		return entityDefinition;
	}

	public static void nullLoader() {
		EntityDefinition.npcCache = null;
		EntityDefinition.streamIndices = null;
		EntityDefinition.cache = null;
		EntityDefinition.buffer = null;
	}

	public static void unpackConfig(Archive archive) {
		EntityDefinition.buffer = new Packet(archive.getFile("npc.dat"));
		Packet index = new Packet(archive.getFile("npc.idx"));
		int totalNPCs = index.getUnsignedShort();
		EntityDefinition.streamIndices = new int[totalNPCs];
		int i = 2;
		for (int j = 0; j < totalNPCs; j++) {
			EntityDefinition.streamIndices[j] = i;
			i += index.getUnsignedShort();
		}
		EntityDefinition.cache = new EntityDefinition[20];
		for (int k = 0; k < 20; k++) {
			EntityDefinition.cache[k] = new EntityDefinition();
		}
	}

	public Model method160() {
		if (childrenIDs != null) {
			EntityDefinition entityDefinition = method161();
			if (entityDefinition == null) {
				return null;
			} else {
				return entityDefinition.method160();
			}
		}
		if (dialogueModels == null) {
			return null;
		}
		boolean flag1 = false;
		for (int i = 0; i < dialogueModels.length; i++) {
			if (!Model.method463(dialogueModels[i])) {
				flag1 = true;
			}
		}
		if (flag1) {
			return null;
		}
		Model models[] = new Model[dialogueModels.length];
		for (int j = 0; j < dialogueModels.length; j++) {
			models[j] = Model.method462(dialogueModels[j]);
		}
		Model model;
		if (models.length == 1) {
			model = models[0];
		} else {
			model = new Model(models.length, models);
		}
		if (originalModelColors != null) {
			for (int k = 0; k < originalModelColors.length; k++) {
				model.method476(originalModelColors[k], modifiedModelColors[k]);
			}
		}
		return model;
	}

	public EntityDefinition method161() {
		int j = -1;
		if (anInt57 != -1) {
			VarBit varBit = VarBit.cache[anInt57];
			int k = varBit.anInt648;
			int l = varBit.anInt649;
			int i1 = varBit.anInt650;
			int j1 = Client.anIntArray1232[i1 - l];
			j = EntityDefinition.clientInstance.variousSettings[k] >> l & j1;
		} else if (anInt59 != -1) {
			j = EntityDefinition.clientInstance.variousSettings[anInt59];
		}
		if ((j < 0) || (j >= childrenIDs.length) || (childrenIDs[j] == -1)) {
			return null;
		} else {
			return EntityDefinition.forID(childrenIDs[j]);
		}
	}

	public Model method164(int j, int k, int ai[]) {
		if (childrenIDs != null) {
			EntityDefinition entityDefinition = method161();
			if (entityDefinition == null) {
				return null;
			} else {
				return entityDefinition.method164(j, k, ai);
			}
		}
		Model model = (Model) EntityDefinition.npcCache.get(type);
		if (model == null) {
			boolean flag = false;
			for (int i1 = 0; i1 < this.models.length; i1++) {
				if (!Model.method463(this.models[i1])) {
					flag = true;
				}
			}
			if (flag) {
				return null;
			}
			Model models[] = new Model[this.models.length];
			for (int j1 = 0; j1 < this.models.length; j1++) {
				models[j1] = Model.method462(this.models[j1]);
			}
			if (models.length == 1) {
				model = models[0];
			} else {
				model = new Model(models.length, models);
			}
			if (originalModelColors != null) {
				for (int k1 = 0; k1 < originalModelColors.length; k1++) {
					model.method476(originalModelColors[k1], modifiedModelColors[k1]);
				}
			}
			model.method469();
			model.preProcess(64 + anInt85, 850 + anInt92, -30, -50, -30, true);
			EntityDefinition.npcCache.put(model, type);
		}
		Model model2 = Model.model;
		model2.method464(model, FrameReader.method532(k) & FrameReader.method532(j));
		if ((k != -1) && (j != -1)) {
			model2.method471(ai, j, k);
		} else if (k != -1) {
			model2.method470(k);
		}
		if ((anInt91 != 128) || (anInt86 != 128)) {
			model2.method478(anInt91, anInt91, anInt86);
		}
		model2.method466();
		model2.anIntArrayArray1658 = null;
		model2.anIntArrayArray1657 = null;
		if (aByte68 == 1) {
			model2.aBoolean1659 = true;
		}
		return model2;
	}

	private void readValues(Packet buffer) {
		do {
			int i = buffer.getUnsignedByte();
			if (i == 0) {
				return;
			}
			if (i == 1) {
				int modelCount = buffer.getUnsignedByte();
				this.models = new int[modelCount];
				for (int id = 0; id < modelCount; id++) {
					this.models[id] = buffer.getUnsignedShort();
				}
			} else if (i == 2) {
				name = buffer.getString();
			} else if (i == 3) {
				description = buffer.getBytes();
			} else if (i == 12) {
				aByte68 = buffer.getByte();
			} else if (i == 13) {
				standAnim = buffer.getUnsignedShort();
			} else if (i == 14) {
				walkAnim = buffer.getUnsignedShort();
			} else if (i == 17) {
				walkAnim = buffer.getUnsignedShort();
				anInt58 = buffer.getUnsignedShort();
				anInt83 = buffer.getUnsignedShort();
				anInt55 = buffer.getUnsignedShort();
			} else if ((i >= 30) && (i < 40)) {
				if (actions == null) {
					actions = new String[5];
				}
				actions[i - 30] = buffer.getString();
				if (actions[i - 30].equalsIgnoreCase("hidden")) {
					actions[i - 30] = null;
				}
			} else if (i == 40) {
				int k = buffer.getUnsignedByte();
				originalModelColors = new int[k];
				modifiedModelColors = new int[k];
				for (int k1 = 0; k1 < k; k1++) {
					originalModelColors[k1] = buffer.getUnsignedShort();
					modifiedModelColors[k1] = buffer.getUnsignedShort();
				}
			} else if (i == 60) {
				int l = buffer.getUnsignedByte();
				dialogueModels = new int[l];
				for (int l1 = 0; l1 < l; l1++) {
					dialogueModels[l1] = buffer.getUnsignedShort();
				}
			} else if (i == 90) {
				buffer.getUnsignedShort();
			} else if (i == 91) {
				buffer.getUnsignedShort();
			} else if (i == 92) {
				buffer.getUnsignedShort();
			} else if (i == 93) {
				aBoolean87 = false;
			} else if (i == 95) {
				combatLevel = buffer.getUnsignedShort();
			} else if (i == 97) {
				anInt91 = buffer.getUnsignedShort();
			} else if (i == 98) {
				anInt86 = buffer.getUnsignedShort();
			} else if (i == 99) {
				aBoolean93 = true;
			} else if (i == 100) {
				anInt85 = buffer.getByte();
			} else if (i == 101) {
				anInt92 = buffer.getByte() * 5;
			} else if (i == 102) {
				anInt75 = buffer.getUnsignedShort();
			} else if (i == 103) {
				anInt79 = buffer.getUnsignedShort();
			} else if (i == 106) {
				anInt57 = buffer.getUnsignedShort();
				if (anInt57 == 65535) {
					anInt57 = -1;
				}
				anInt59 = buffer.getUnsignedShort();
				if (anInt59 == 65535) {
					anInt59 = -1;
				}
				int i1 = buffer.getUnsignedByte();
				childrenIDs = new int[i1 + 1];
				for (int i2 = 0; i2 <= i1; i2++) {
					childrenIDs[i2] = buffer.getUnsignedShort();
					if (childrenIDs[i2] == 65535) {
						childrenIDs[i2] = -1;
					}
				}
			} else if (i == 107) {
				aBoolean84 = false;
			}
		} while (true);
	}

	private EntityDefinition() {
		anInt55 = -1;
		anInt57 = -1;
		anInt58 = -1;
		anInt59 = -1;
		combatLevel = -1;
		walkAnim = -1;
		aByte68 = 1;
		anInt75 = -1;
		standAnim = -1;
		type = -1L;
		anInt79 = 32;
		anInt83 = -1;
		aBoolean84 = true;
		anInt86 = 128;
		aBoolean87 = true;
		anInt91 = 128;
		aBoolean93 = false;
	}
}
